import os
import pandas
import config
import utils

import pandas as pd

class BKDproduct(object):
    """
    Product class
    """
    def __init__(self):
        self.code: str = "" 
        self.name: str = ""
        self.batch: str = ""

    def test_report(self):
        # 读取测试结果
        df_t = pd.read_csv(os.path.join(config.DATA_PATH,'test report.csv'))
        # 筛选出当前batch的测试结果
        df_t = df_t[df_t['BKD Batch*']==self.batch].reset_index(drop=True)
        # 规范化列
        df_t.columns =['code','batch','product_name','test_items','test_result','target','result','coa_link','parent_item','id']
        # 取出product code
        productcode = df_t.loc[0,'code']
        # 删除测试项目为空的行
        df_t.dropna(subset='test_items',inplace=True)
        return productcode,df_t

    def prepare_coa_spec(productcode):
        df_product = utils.validate_product_info()
        df_product = df_product.copy()
        if productcode in df_product["Product Code"].unique():
            df_product = df_product[df_product["Product Code"] == productcode]
            df_product = df_product.reset_index(drop=True)
        else:
            print("!Error. No such code!")
        return df_product

class BKDcoa(BKDproduct):
    def test_report(self):
        # 读取测试结果
        df_t = pd.read_csv(os.path.join(config.DATA_PATH,'test report.csv'))
        # 筛选出当前batch的测试结果
        df_t = df_t[df_t['BKD Batch*']==self.batch].reset_index(drop=True)
        # 规范化列
        df_t.columns =['code','batch','product_name','test_items','test_result','target','result','coa_link','parent_item','id']
        # 取出product code
        productcode = df_t.loc[0,'code']
        # 删除测试项目为空的行
        df_t.dropna(subset='test_items',inplace=True)
        return productcode,df_t
    
    def coa_target(productcode):
        df_target = pd.read_csv(os.path.join(config.DATA_PATH,'coa target.csv'))
        df_target = df_target[df_target['product_code']==productcode]
        return df_target